package com.zjhome.springcloud.admin.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.zjhome.itoken.common.domain.TbSysUser;
import com.zjhome.itoken.common.dto.BaseResult;
import com.zjhome.springcloud.admin.service.AdminService;


@RestController
//@RequestMapping("")
public class AdminController {
	@Autowired
	private AdminService adminService;
	
	@RequestMapping(value="/login", method=RequestMethod.GET)
	public BaseResult login(String logincode, String password) {
		BaseResult result = checkLogin(logincode, password);
		if (result != null) {
			return result;
		}
		
		TbSysUser user = adminService.login(logincode, password);
		if (user != null) {
			// 登陆成功
			return BaseResult.ok(user);
		}
		else {
			// 登陆失败
			return BaseResult.notOk(Lists.newArrayList(
					new BaseResult.Error("fail", "登陆用户名或密码不正确")
					));
		}		
	}
	
	private BaseResult checkLogin(String logincode, String password) {		
		if (StringUtils.isBlank(logincode) || StringUtils.isBlank(password)) {
			return BaseResult.notOk(Lists.newArrayList(
					new BaseResult.Error("logincode", "登陆用户不能为空"),
					new BaseResult.Error("password", "输入密码不能为空")
					));
		}
		return null;
	}
	
}
